package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    RedisTemplate redisTemplate;
    /**
     * 新增套餐
     */
    @Transactional
    @Override
    public void save(SetmealDTO setmealDTO) {
        //封装SetmealDTO类对象
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //将对象内状态设置为起售
        setmeal.setStatus(StatusConstant.ENABLE);
        //填装setmeal表数据
        setmealMapper.save(setmeal);

        setmealDTO.getSetmealDishes().stream().map(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
            //添加中间表数据
            setmealDishMapper.save(setmealDish);
            return setmealDish;
        }).collect(Collectors.toList());
    }

    /**
     * 根据id查询套餐
     */
    @Override
    public SetmealVO setmealById(Long id) {
        //查询setmeal表
        Setmeal setmeal = setmealMapper.setmealById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        //查询setmealDish表 给setmealVO 的SetmealDishes集合赋值
        List<SetmealDish> setmealDishes = setmealDishMapper.setmealById(setmeal.getId());
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }
    /**
     * 分页查询
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<Setmeal> page = setmealMapper.page(setmealPageQueryDTO);
        List<SetmealVO> collect = page.getResult().stream().map(setmeal -> {
            SetmealVO setmealVO = new SetmealVO();
            BeanUtils.copyProperties(setmeal, setmealVO);
            //查询category表，获取分类名
            Category page1 = categoryMapper.page(setmeal);
            setmealVO.setCategoryName(page1.getName());
            return setmealVO;
        }).collect(Collectors.toList());

        return new PageResult(page.getTotal(),collect);
    }

    /**
     * 起售 停售
     * @param status
     * @param id
     */
    @Override
    public void upDate(Integer status, Long id) {
        //当套餐起售时，如果内部有菜品禁止 则报异常
        if (status == 1) {
            List<SetmealDish> setmealDishes = setmealDishMapper.setmealById(id);
            for (SetmealDish setmealDish : setmealDishes) {
                Dish dish = dishMapper.upById(setmealDish.getDishId());
                if (dish.getStatus()==0) {
                    throw new BaseException("套餐内有为起售菜品");
                }
            }
        }
        setmealMapper.upDate(status,id);
    }

    /**
     * 删除
     * @param ids
     */

    @Override
    public void delSetmeal(List<Long> ids) {
        //判断是否为起售状态
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.setmealById(id);
            if (setmeal.getStatus()==1) {
                throw new BaseException("套餐正在起售");
            }
        }
        setmealDishMapper.delSetmeal(ids);
        setmealMapper.delSetmeal(ids);

        Set keys = redisTemplate.keys("DISH_*");
        redisTemplate.delete(keys);
    }
    @Transactional
    @Override
    public void upSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //修改setmeal表
        setmealMapper.upSetmeal(setmeal);
        setmealDTO.getSetmealDishes().stream().map(setmealDish -> {
            //修改setmealDish表内容
            setmealDishMapper.upSetmeal(setmealDish);
            return setmealDish;
        }).collect(Collectors.toList());

        Set keys = redisTemplate.keys("DISH_*");
        redisTemplate.delete(keys);
    }

    @Override
    public List<Setmeal> list(SetmealDTO setmealDTO) {

        return setmealMapper.list(setmealDTO);
    }

    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        //根据套餐id查询绑定的菜品信息
        List<SetmealDish> setmealDishes = setmealDishMapper.findBySetmealId(id);
        List<DishItemVO> dishItemList = setmealDishes.stream().map(setmealDish -> {
            Long dishId = setmealDish.getDishId();
            //根据菜品id查询菜品信息
            Dish dish = dishMapper.findById(dishId);
            //封装DishItem
            DishItemVO dishItemVO = new DishItemVO();
            dishItemVO.setImage(dish.getImage());
            dishItemVO.setDescription(dish.getDescription());
            dishItemVO.setCopies(setmealDish.getCopies());
            dishItemVO.setName(setmealDish.getName());
            return dishItemVO;
        }).collect(Collectors.toList());

        return dishItemList;
    }
}
